/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.gui;

import org.openeccos.util.PDUtil;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.ApplicationInstance;
import nextapp.echo2.app.Border;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Component;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.FillImage;
import nextapp.echo2.app.Font;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.layout.RowLayoutData;
import nextapp.echo2.contrib.app.effect.ScaleEffectCommand;
import echopointng.ButtonEx;
import echopointng.ContainerEx;

/**
 * A lateral panel which slides into the PDQuickle
 *
 * @author cgspinner@web.de
 */
public class PDQuickleSlider extends ContainerEx {

	private ButtonEx btnExpand;
	private boolean expanded = false;
	private Grid grid;
	private String label;

	public PDQuickleSlider(String label) {
		this.label = label;
		initGUI();
	}

	private void initGUI() {
		setLeft(new Extent(-301)); // !!!!!
		setWidth(new Extent(20));
		setBorder(new Border(new Extent(0), null, Border.STYLE_NONE));
		setBackground(Color.ORANGE);

		// this row contains in this order: the pusher, the filter grid and the
		// show/hide button
		// the pusher (100px) and the grid (300px) are initially invisible,
		// as the x-coordinates are moved 400px to the left (see above)
		// the pusher expands from 100px to 400px (=400%), and pushes the grid
		// into the frame
		final Row row = new Row();
		add(row);

		// this component is never visible
		// it is just there to use more or less space
		final ContainerEx pusher = new ContainerEx();
		pusher.setWidth(new Extent(100));
		row.add(pusher);

		row.add(addGrid());

		btnExpand = new ButtonEx(label);
		btnExpand.setBackground(Color.ORANGE);
		btnExpand.setForeground(Color.WHITE);
		btnExpand.setInsets(new Insets(1, 1, 0, 0));
		btnExpand.setBorder(new Border(new Extent(0), null, Border.STYLE_NONE));
		btnExpand.setHeight(new Extent(25));
		btnExpand.setWidth(new Extent(11));
		btnExpand.setRolloverEnabled(false);
		btnExpand.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ScaleEffectCommand cmd = new ScaleEffectCommand();
				expanded = !expanded;
				if (expanded) {
					cmd.setStartScalePercentage(100f);
					cmd.setEndScalePercentage(295);
				} else {
					cmd.setStartScalePercentage(100f);
					cmd.setEndScalePercentage(34);
				}
				cmd.setComponent(pusher);
				ApplicationInstance.getActive().enqueueCommand(cmd);
			}
		});
		RowLayoutData rld = new RowLayoutData();
		rld.setAlignment(new Alignment(Alignment.TOP, Alignment.TOP));
		btnExpand.setLayoutData(rld);
		row.add(btnExpand);
	}

	private Grid addGrid() {
		grid = new Grid(2);
		grid.setInsets(new Insets(15, 4, 2, 4));
		grid.setWidth(new Extent(194));
		RowLayoutData rld = new RowLayoutData();
		rld.setBackgroundImage(new FillImage(PDUtil.getImage("/img/semitrans4.png")));
		rld.setAlignment(new Alignment(Alignment.TOP, Alignment.TOP));
		rld.setInsets(new Insets(3));
		grid.setLayoutData(rld);
		return grid;
	}

	public void addGridElement(String caption, Component c) {
		Label lbl = new Label(caption);
		lbl.setFont(new Font(Font.VERDANA, Font.PLAIN, new Extent(10)));
		grid.add(lbl);
		c.setFont(new Font(Font.VERDANA, Font.PLAIN, new Extent(10)));
		grid.add(c);
	}
}
